package cn.yilongquan.controller;

import cn.yilongquan.authorize.PreAuthorize;
import cn.yilongquan.user.query.MenuQuery;
import cn.yilongquan.service.MenuService;
import cn.yilongquan.user.domain.Menu;
import cn.yilongquan.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单控制器
 */
@Api(value = "menu相关的接口", tags = "菜单管理")
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 根据ID查询菜单
     */
    @ApiOperation(value = "查询指定菜单信息")
    @GetMapping("/{id}")
    @PreAuthorize(sn = "menu:findById", name = "根据ID查询菜单", descs = "根据ID查询指定菜单的详细信息")
    public R findById(@PathVariable("id") Long id) {
        return R.ok(menuService.findById(id));
    }

    /**
     * 查询所有菜单
     */
    @ApiOperation(value = "查询所有菜单信息")
    @GetMapping
    @PreAuthorize(sn = "menu:findAll", name = "查询所有菜单", descs = "查询系统中所有的菜单信息")
    public R findAll() {
        return R.ok(menuService.findAll());
    }

    /**
     * 根据ID删除菜单
     */
    @ApiOperation(value = "删除指定菜单信息")
    @DeleteMapping("/{id}")
    @PreAuthorize(sn = "menu:deleteById", name = "根据ID删除菜单", descs = "根据ID删除指定的菜单")
    public R deleteById(@PathVariable("id") Long id) {
        try {
            menuService.deleteById(id);
            return R.ok();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 批量删除菜单
     */
    @ApiOperation(value = "批量删除菜单信息")
    @PostMapping("/deleteAll")
    @PreAuthorize(sn = "menu:batchDelete", name = "批量删除菜单", descs = "根据ID列表批量删除菜单")
    public R batchDelete(@RequestBody List<Long> ids) {
        try {
            menuService.batchDelete(ids);
            return R.ok();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 添加或修改菜单
     */
    @ApiOperation(value = "添加或修改菜单信息")
    @PostMapping("/add")
    @PreAuthorize(sn = "menu:addOrUpdate", name = "添加或修改菜单", descs = "新增菜单或更新已有菜单信息")
    public R add(@RequestBody Menu menu) {
        try {
            if (menu.getId() == null) {
                // 新增操作
                menuService.add(menu);
            } else {
                // 修改操作
                if (menu.getId().equals(menu.getParentId())) {
                    return R.fail("不能选择自己作为父级菜单");
                }
                menuService.update(menu);
            }
            return R.ok();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 分页查询菜单
     */
    @ApiOperation(value = "分页查询菜单信息")
    @PostMapping("/list")
    @PreAuthorize(sn = "menu:pageList", name = "分页查询菜单", descs = "根据查询条件分页查询菜单信息")
    public R pageList(@RequestBody MenuQuery menuQuery) {
        return menuService.pageList(menuQuery);
    }

    /**
     * 获取菜单树形结构
     */
    @ApiOperation(value = "获取菜单树形结构")
    @GetMapping("/getTree")
    @PreAuthorize(sn = "menu:getTree", name = "获取菜单树形结构", descs = "查询菜单的树形结构数据")
    public R getTree() {
        return menuService.getTree();
    }
}
